<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Config;
use App\Services\SitemapService;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    protected function success($msg,$sub_msg='',$redirect=''){
        if(request()->ajax()){
            return json_encode(['type'=>'tips','msg'=>$msg,'sub_msg'=>$sub_msg,'code'=>200,'redirect'=>$redirect,'form_data'=>request()->except('_token','_method')],JSON_UNESCAPED_UNICODE);
        }else{
            session()->flash('flash',json_encode(['type'=>'tips','msg'=>$msg,'sub_msg'=>$sub_msg,'code'=>200,'form_data'=>request()->except('_token','_method')],JSON_UNESCAPED_UNICODE));

            if($redirect){
                return redirect($redirect);
            }
        }
    }

    protected function error($msg,$sub_msg='',$redirect=''){
        if(request()->ajax()){
            return json_encode(['type'=>'tips','msg'=>$msg,'sub_msg'=>$sub_msg,'code'=>400,'redirect'=>$redirect,'form_data'=>request()->except('_token','_method')],JSON_UNESCAPED_UNICODE);
        }else{
            session()->flash('flash',json_encode(['type'=>'tips','sub_msg'=>$sub_msg,'msg'=>$msg,'code'=>400,'form_data'=>request()->except('_token','_method')],JSON_UNESCAPED_UNICODE));

            if($redirect){

                return redirect($redirect);
            }
        }
    }

    public function robots(){
        $config = Config::where('name','robots')->first();
        if($config){
            return response($config->content)->header('Content-type','text/plain');
        }else{
            return response('')->header('Content-type','text/plain');
        }
    }

    public function sitemap(){
        $xml = app(SitemapService::class)->generate();
        return response($xml)->header('Content-type','text/xml');
    }
}
